<template>
	<div>
		<div style="padding: 20px;width: 100%;height: 100%;">
			<div style="float: right;margin-top: -15px;">
				<Button class="row-btn" style="margin-right: 10px; height: 32px; line-height: 32px; background-color: #ececec;"
				 type="text" @click="purgeDownloadResult">
					<Icon type="ios-skip-forward" size="18" />清空所有下载列表</Button>
			</div>
		</div>
		<div style="padding: 10px;">
			<Table border :columns="columns12" :data="data" :show-header="false" :showBorder="false" highlight-row>
				<template slot-scope="{ row }" slot="name">
					<div style="height: 10px;"></div>
					<Icon type="ios-albums" size="20" style="float: left;padding-right: 5px;" />
					<h3>{{ (row.files[0].path).substring((row.files[0].path).lastIndexOf('/') + 1, (row.files[0].path).length) }}</h3>
					<strong></strong>
					<Progress :percent="(Number((row.completedLength / row.totalLength * 100).toFixed(2)))" />
					<div style="height: 20px;"></div>
				</template>
				<template slot-scope="{ row, index }" slot="action">
					<ButtonGroup shape="circle">
						<Button icon="md-link" @click="copyLink(index)" v-clipboard:copy="row.files[0].uris[0].uri" v-clipboard:success="onCopy"
						 v-clipboard:error="onError"></Button>
						<Button icon="ios-folder-open" @click="folder(index)"></Button>
						<Button icon="md-alert" @click="details(index)"></Button>
						<Button icon="md-close" @click="Stop(index)"></Button>
					</ButtonGroup>
				</template>
			</Table>
		</div>
	</div>
</template>
<script>
	import axios from 'axios'
	export default {
		data() {
			return {
				columns12: [{
						title: 'Name',
						slot: 'name'
					},
					{
						title: 'Action',
						slot: 'action',
						width: 200,
						align: 'center'
					}
				],
				data: []
			}
		},
		mounted() {
			this.fjhv()
		},
		methods: {
			onCopy(e) {
				console.log('复制成功！');
			},
			onError(e) {
				console.log('复制失败！');
			},
			show(index) {
				this.$Modal.info({
					title: 'User Info',
					content: `Name：${this.data6[index].name}<br>Age：${this.data6[index].age}<br>Address：${this.data6[index].address}`
				})
			},
			remove(index) {
				console.log(this.data6[index].age)
			},
			fjhv() {
				var url = 'http://127.0.0.1:6800/jsonrpc'
				var json_rpc1 = {
					id: '',
					jsonrpc: '2.0',
					method: 'aria2.tellStopped',
					params: ['token:kumao', 0, 5]
				};

				var interval =  setInterval((res => {
					if(this.$route.path == '/Completed' ){
					axios({
						url: url,
						method: 'post',
						crossDomain: true,
						processData: false,
						data: JSON.stringify(json_rpc1),
						headers: {
							contentType: 'application/json'
						}
					}).then((respanse) => {
						var _this = this
						this.data = respanse.data.result
					})
					}else{
						clearInterval(interval);
					}

				}), 2000);

			},
			purgeDownloadResult() {
				var url = 'http://127.0.0.1:6800/jsonrpc'
				var json_rpc1 = {
					id: '',
					jsonrpc: '2.0',
					method: 'aria2.purgeDownloadResult',
					params: ['token:kumao']
				};

				axios({
					url: url,
					method: 'post',
					crossDomain: true,
					processData: false,
					data: JSON.stringify(json_rpc1),
					headers: {
						contentType: 'application/json'
					}
				}).then((respanse) => {
					console.log(respanse)
					var _this = this
					this.data = respanse.data.result
					console.log('清理成功')
				})


			},
			folder(index) {
				var exec = require('child_process').exec;
				exec('start ' + (this.data[index].dir).substring((this.data[index].dir).indexOf('/') + 1, (this.data[index].dir).length)
					.split('/').join('\\'))
			},
			copyLink(index) {
				console.log(this.data[index].files[0].uris[0].uri)
				this.$message({
					message: '恭喜你，复制成功~！',
					type: 'success',
					center: true
				});
			},
			details(index) {
				this.$alert('<strong>GID：' + this.data[index].gid + ' </strong><br/>' + '<strong>已完成的下载：：' + (this.data[index].completedLength /
						1000000).toFixed(2) + 'MB' +
					' </strong><br/>' + '<strong>文件总量大小：' + (this.data[index].totalLength / 1000000).toFixed(2) + 'MB' +
					' </strong><br/>' + '<strong>文件保存目录：' +
					this.data[index].dir + ' </strong><br/>', '详细信息：' + (this.data[index].files[0].path).substring((this.data[index].files[
						0].path).lastIndexOf('/') + 1, (this.data[index].files[0].path).length), {
						dangerouslyUseHTMLString: true
					});
			},
			//关闭下载文件
			Stop(index) {
				var url = 'http://127.0.0.1:6800/jsonrpc'
				var json_rpc1 = {
					id: '',
					jsonrpc: '2.0',
					method: 'aria2.remove',
					params: [this.data[index]]
				};
			
				axios({
					url: url,
					method: 'post',
					crossDomain: true,
					processData: false,
					data: JSON.stringify(json_rpc1),
					headers: {
						contentType: 'application/json'
					}
				}).then((respanse) => {})
			},
			del(index) {

				var url = 'http://127.0.0.1:6800/jsonrpc'
				var json_rpc1 = {
					id: '',
					jsonrpc: '2.0',
					method: 'aria2.purgeDownloadResult',
					params: ['token:kumao']
				};
				axios({
					url: url,
					method: 'post',
					crossDomain: true,
					processData: false,
					data: JSON.stringify(json_rpc1),
					headers: {
						contentType: 'application/json'
					}
				}).then((respanse) => {
					console.log(respanse)
				})
			},
			Paused() {

			}


		}
	}
</script>
